package com.hzdl.apple.dao.imp;

import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.hzdl.apple.bean.User;
import com.hzdl.apple.dao.BaseDaoImp;
import com.hzdl.apple.dao.UserDao;
import com.hzdl.apple.utils.DruidUtils;


public class UserDaoImp extends BaseDaoImp implements UserDao {
	private QueryRunner qr;
	private String sql;

	public UserDaoImp() {
		qr = new QueryRunner(DruidUtils.dataSource);
	}
	@Override
	public User getUser(String id, String psw) {
		sql="select * from user where ULoginID=? and ULoginPsw=? ";
		try {
			return qr.query(sql, new BeanHandler<>(User.class),id,psw);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public boolean addUser(User user) {
		sql = "insert into user values(?,?,?,?,?,?)";
		System.out.println(user);
		try {
			qr.update(sql, null, user.getUName(),user.getULoginId(), user.getULoginPsw(),  user.getUAddress(),user.getUPhoneNum());
			return true;
		} catch (SQLException e) {
			
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public boolean isExist(String id) {
		sql="select * from user where ULoginID=?";
		try {
			User user=qr.query(sql, new BeanHandler<>(User.class),id);
			if (user!=null) {
				return true;
			}
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return false;
	}
	@Override
	public void changePassword(User user,String newPassword) {
		String sql="update user set ULoginPsw=? where UID=?";
		
		try {
			qr.update(sql, newPassword,user.getUID());
		} catch (SQLException e) {
		
			e.printStackTrace();
		}
		
	}
	@Override
	public void UpdateUser(String uName,String uMessage) {
		String sql="update user UMessage=? where UName=?";
		try {
			qr.update(sql, uMessage,uName);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	}
	@Override
	public User showUser(String UName) {
		String sql="select * from user where UName=?";
		try {
			return qr.query(sql, new BeanHandler<>(User.class),UName);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public User getUserPsw(String uname, String uphone) {
		sql="select * from user where UName=? and UPhoneNum=? ";
		try {
			return qr.query(sql, new BeanHandler<>(User.class),uname,uphone);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return null;
	}
	
	
}
